<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_commandes = valueExists((int)$_GET['id'], "CODECOMMANDEEXTERNE", "commande_externe", $connexion);
			
			if($nb_commandes == 1){ // la commande à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner une seule commande
	- La commande doit exister dans la BDD

*****************************************/

	// récupération des informations de la commande
	$query_recuperation_commande = "SELECT ca.DATECOMMANDE AS date, ca.CONVENTIONSIGNEE AS convention, ca.COMMENTAIRE AS commentaire, ca.CODESTRUCTURE AS idStructure, s.NOMSTRUCTURE AS structure, s.ADRESSESTRUCTURE AS adresse, cu.NOMCOMMUNE AS commune, cu.CODEPOSTAL AS cp, pa.NOMPAYS AS pays, fam.NOMFAMILLE AS famille, gen.NOMGENRE AS genre, esp.NOMESPECE AS espece, var.NOMVARIETE AS variete, sa.CODESACHETGRAINE AS idSachet, sa.REFERENCESACHETGRAINES AS refSachet, se.CODESEMIS AS idSemis, se.REFERENCESEMIS AS refSemis, tou.CODETOUFFE AS idTouffe, tou.REFERENCETOUFFE AS refTouffe, li.CODESORTIE AS idSortie ";
	$query_recuperation_commande .= "FROM commande_externe ca ";
	$query_recuperation_commande .= "INNER JOIN structure s ON s.CODESTRUCTURE = ca.CODESTRUCTURE ";
	$query_recuperation_commande .= "LEFT JOIN commune cu ON cu.CODECOMMUNE = s.CODECOMMUNE ";
	$query_recuperation_commande .= "LEFT JOIN pays pa ON pa.CODEPAYS = cu.CODEPAYS ";
	$query_recuperation_commande .= "LEFT JOIN sortie li ON li.CODECOMMANDEEXTERNE = ca.CODECOMMANDEEXTERNE ";
	$query_recuperation_commande .= "LEFT JOIN sachet_graines sa ON li.CODESACHETGRAINE = sa.CODESACHETGRAINE ";
	$query_recuperation_commande .= "LEFT JOIN semis se ON li.CODESEMIS = se.CODESEMIS ";
	$query_recuperation_commande .= "LEFT JOIN touffe tou ON li.CODETOUFFE = tou.CODETOUFFE ";
	$query_recuperation_commande .= "LEFT JOIN taxon t ON t.CODETAXON = sa.CODETAXON OR t.CODETAXON = se.CODETAXON OR t.CODETAXON = tou.CODETAXON ";
	$query_recuperation_commande .= "LEFT JOIN est_de_famille l1 INNER JOIN famille fam ON fam.CODEFAMILLE = l1.CODEFAMILLE ON l1.CODETAXON = t.CODETAXON AND l1.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN appartient_a_genre l2 INNER JOIN genre gen ON gen.CODEGENRE = l2.CODEGENRE ON l2.CODETAXON = t.CODETAXON AND l2.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN est_de_espece l3 INNER JOIN espece esp ON esp.CODEESPECE = l3.CODEESPECE ON l3.CODETAXON = t.CODETAXON AND l3.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN a_comme_variete l4 INNER JOIN variete var ON var.CODEVARIETE = l4.CODEVARIETE ON l4.CODETAXON = t.CODETAXON AND l4.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "WHERE ca.CODECOMMANDEEXTERNE = ".$_GET['id']." ";
	
	$result_recuperation_commande = mysql_query($query_recuperation_commande, $connexion) or logError("RECUPERATION COMMANDE EXTERNE-".$query_recuperation_commande."-".mysql_error());
	
	$commande = array(); // on créé un tableau contenant toutes les informations de la commande
	while($tab_recuperation_commande = mysql_fetch_assoc($result_recuperation_commande)){
	
		$commande['date'] = $tab_recuperation_commande['date'];
		$commande['convention'] = (bool)$tab_recuperation_commande['convention'];
		$commande['commentaire'] = $tab_recuperation_commande['commentaire'];
		$commande['structure']['id'] = $tab_recuperation_commande['idStructure'];
		$commande['structure']['nom'] = $tab_recuperation_commande['structure'];
		$commande['structure']['adresse'] = $tab_recuperation_commande['adresse'];
		$commande['structure']['commune'] = $tab_recuperation_commande['commune'];
		$commande['structure']['cp'] = $tab_recuperation_commande['cp'];
		$commande['structure']['pays'] = $tab_recuperation_commande['pays'];
		
		if(!is_null($tab_recuperation_commande['idSortie'])){
			if(!is_null($tab_recuperation_commande['idSachet'])){
				$commande['envois'][$tab_recuperation_commande['idSortie']]['sachet']['id'] = $tab_recuperation_commande['idSachet'];
				$commande['envois'][$tab_recuperation_commande['idSortie']]['sachet']['reference'] = $tab_recuperation_commande['refSachet'];
			}
			if(!is_null($tab_recuperation_commande['idSemis'])){
				$commande['envois'][$tab_recuperation_commande['idSortie']]['semis']['id'] = $tab_recuperation_commande['idSemis'];
				$commande['envois'][$tab_recuperation_commande['idSortie']]['semis']['reference'] = $tab_recuperation_commande['refSemis'];
			}
			if(!is_null($tab_recuperation_commande['idTouffe'])){
				$commande['envois'][$tab_recuperation_commande['idSortie']]['touffe']['id'] = $tab_recuperation_commande['idTouffe'];
				$commande['envois'][$tab_recuperation_commande['idSortie']]['touffe']['reference'] = $tab_recuperation_commande['refTouffe'];
			}
			$commande['envois'][$tab_recuperation_commande['idSortie']]['taxon']['famille'] = $tab_recuperation_commande['famille'];
			$commande['envois'][$tab_recuperation_commande['idSortie']]['taxon']['genre'] = $tab_recuperation_commande['genre'];
			$commande['envois'][$tab_recuperation_commande['idSortie']]['taxon']['espece'] = $tab_recuperation_commande['espece'];
			$commande['envois'][$tab_recuperation_commande['idSortie']]['taxon']['variete'] = $tab_recuperation_commande['variete'];
		}
	
	}
	
	mysql_free_result($result_recuperation_commande);
	
?>
<h3>Détail d'une requête</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<td colspan="2">
			Requête du <b><?php echo dateFr($commande['date']); ?></b> de <b><?php echo htmlentities($commande['structure']['nom'], ENT_NOQUOTES, "UTF-8"); ?></b>
		</td>
	</tr>
	<tr>
		<th colspan="2">Informations</th>
	</tr>
	<tr>
		<td style="width: 20%;" class="etiquette">De:</td>
		<td style="width: 80%;">
			<?php echo "<a href=\"popup.php?p=gestion/structure_affiche_popup&amp;id=".$commande['structure']['id']."\" onclick=\"popup(this.href, 400, 500); return false;\">".htmlentities($commande['structure']['nom'], ENT_NOQUOTES, "UTF-8")."</a>"; ?><br />
			<?php echo htmlentities($commande['structure']['adresse'], ENT_NOQUOTES, "UTF-8"); ?><br />
			<?php echo htmlentities($commande['structure']['cp'], ENT_NOQUOTES, "UTF-8"); ?> - <?php echo htmlentities($commande['structure']['commune'], ENT_NOQUOTES, "UTF-8"); ?> (<?php echo htmlentities(mb_strtoupper($commande['structure']['pays']), ENT_NOQUOTES, "UTF-8"); ?>)
		</td>
	</tr>
	<tr>
		<td class="etiquette">Conv. signée:</td>
		<td><img src="images/<?php echo boolToText((bool)$commande['convention']); ?>.png" alt="<?php echo boolToText((bool)$commande['convention']); ?>" /></td>
	</tr>
	<tr>
		<th colspan="2">Envois</th>
	</tr>
	<tr>
		<td colspan="2">
			<?php
				if(isset($commande['envois'])){
					echo "<ul>";
					foreach($commande['envois'] as $envoi){
						echo "<li>";
						if(isset($envoi['sachet'])){
							echo "<b><a href=\"popup.php?p=gestion/sachetgraines_affiche_popup&amp;id=".$envoi['sachet']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Sachet de graines";
							if(!is_null($envoi['sachet']['reference'])){
								echo " (n° ent. ".htmlentities($envoi['sachet']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a></b>";
						}else if(isset($envoi['semis'])){
							echo "<b><a href=\"popup.php?p=gestion/semis_affiche_popup&amp;id=".$envoi['semis']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Semis";
							if(!is_null($envoi['semis']['reference'])){
								echo " (n° ent. ".htmlentities($envoi['semis']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a></b>";
						}else if(isset($envoi['touffe'])){
							echo "<b><a href=\"popup.php?p=gestion/touffe_affiche_popup&amp;id=".$envoi['touffe']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Touffe";
							if(!is_null($envoi['touffe']['reference'])){
								echo " (n° ent. ".htmlentities($envoi['touffe']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a></b>";
						}
						
						echo " - ".htmlentities(capitalise($envoi['taxon']['famille']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($envoi['taxon']['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($envoi['taxon']['espece']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($envoi['taxon']['variete']), ENT_NOQUOTES, "UTF-8");
						echo "</li>";
					}
					unset($envoi);
					echo "</ul>";
				}else{
					echo "<i>aucun</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Commentaire</th>
	</tr>
	<tr>
		<td class="texte" colspan="2"><?php echo nl2br(htmlentities($commande['commentaire'], ENT_NOQUOTES, "UTF-8")); ?></td>
	</tr>
</table>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("La requête n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("La requête n'est pas ou mal indiquée", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>